package com.hemh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hemh.utils.TimeUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value="index项目接口")
public class IndexController {
	
	private final Log logger = LogFactory.getLog(IndexController.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@GetMapping("/index")
	@ApiOperation(value="跳转index首页")
	public String getIndex() {
		logger.info("跳转index首页--thymeleaf");
		return "index";
	}
	
	@RequestMapping(path="/index/msg",  method = RequestMethod.GET)
	@ApiOperation(value="测试项目index消息")
	@ResponseBody
	public String getMessage() {
		logger.info("测试项目index消息");
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("messgae", "success");
        map.put("author", "hemh");
        map.put("time", TimeUtils.getLocalDateTimeNow());
		return map.toString();
	}
	

    @GetMapping("/index/QueryUserList")
	@ApiOperation(value="查询所有User")
	@ResponseBody
    public Map<String,Object> queryList(){
    	logger.info("测试查询List");
        List<Map<String,Object>> list = 
        		jdbcTemplate.queryForList("SELECT * FROM oms_user");
        Map<String,Object> mapResult = new HashMap<String,Object>();
        mapResult.put("resultList", list);
        return mapResult;
    }
    
    @SuppressWarnings("unchecked")
	@PostMapping("/index/QueryOneUser")
	@ApiOperation(value="查询第一个User")
	@ResponseBody
    public Map<String,Object> queryOne(){
    	logger.info("测试查询one");
        List<Map<String,Object>> list = 
        		jdbcTemplate.queryForList("SELECT * FROM oms_user");
        Map<String, Object> mapResult = new HashMap<String, Object>();
        
        if(list==null || list.size()==0) {
        	return (Map<String, Object>) mapResult.put("resultOne", null);
        }
        mapResult.put("resultOne", list.get(0));
        return mapResult;
    }
	
}
